package com.example.parttimejob.controller;

import com.example.parttimejob.mapper.PartTimeJobsMapper;
import com.example.parttimejob.mapper.StoreMapper;
import com.example.parttimejob.pojo.entity.Merchants;
import com.example.parttimejob.pojo.entity.PartTimeJobs;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@RestController
@CrossOrigin
public class merchantscontroller {
    @Autowired
    StoreMapper merchantsMapper;
    @Autowired
    PartTimeJobsMapper partTimeJobsMapper;

    @GetMapping("/Merchants/PartTimeJobsmapper/{jobId}")
    public ResponseEntity<Map<String, Object>> getInfoFromTwo(@PathVariable int jobId) {
        try {
            // 从PartTimeJobs表中获取数据
            PartTimeJobs partTimeJob = partTimeJobsMapper.selectById(jobId);

            if (partTimeJob == null) {
                return ResponseEntity.notFound().build();
            }

// 使用PartTimeJobs中的merchantId从Merchants表中获取数据
            Merchants merchant = merchantsMapper.selectById(partTimeJob.getMerchantId());


            if (merchant == null) {
                return ResponseEntity.notFound().build();
            }

            // 准备响应数据
            Map<String, Object> response = new HashMap<>();
            response.put("salary", partTimeJob.getSalary());
            response.put("descriptions", partTimeJob.getDescriptions());
            response.put("merchantId", partTimeJob.getMerchantId());
            response.put("merchantUsername", merchant.getMerchantUsername());
            response.put("userName", merchant.getUserName());
            response.put("address", merchant.getAddress());
            response.put("contact", merchant.getContact());
            response.put("merchantRating", merchant.getMerchantRating());
            response.put("img",merchant.getImg());
            response.put("merchantName",merchant.getMerchantName());
            return ResponseEntity.ok(response);
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
        }
    }
}